package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.ExamPaper;
import com.think.teaching.service.impl.CourseUserServiceImpl;
import com.think.teaching.service.impl.ExamPaperServiceImpl;
import com.think.teaching.service.impl.TagServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 试卷 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "试卷管理接口")
public class ExamPaperController {
	
	private final ExamPaperServiceImpl examPaperService;

	private final CourseUserServiceImpl courseUserServiceImpl;

	private final TagServiceImpl tagServiceImpl;

	public ExamPaperController(ExamPaperServiceImpl examPaperService,
							   CourseUserServiceImpl courseUserServiceImpl, TagServiceImpl tagService) {
		this.examPaperService = examPaperService;
		this.courseUserServiceImpl = courseUserServiceImpl;
		this.tagServiceImpl =tagService;
	}

	@ApiOperation("添加新的试卷信息")
	@PostMapping("admin/examPaper")
	public Result AddNewExamPaper(@RequestBody()ExamPaper data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			ExamPaper examPaper = examPaperService.AddExamPaper(data);
			if (examPaper != null) {
				return Result.success(examPaper);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新的试卷信息")
	@RequestMapping(value = "admin/examPaper",method = RequestMethod.PUT)
	public Result UpdateExamPaper(@RequestBody()ExamPaper data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			ExamPaper examPaper = examPaperService.UpdateExamPaper(data);
			if (examPaper != null) {
				return Result.success(examPaper);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除试卷信息")
	@RequestMapping(value = "admin/examPaper/{id}",method = RequestMethod.DELETE)
	public Result DeleteExamPaper(@PathVariable("id")Integer id) {
		ExamPaper examPaper = examPaperService.getById(id);
		if (examPaper == null) {
			return Result.failure(ResultCode.EXAM_PAPER_NOT_EXIST);
		} else {
			if (examPaperService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取试卷信息详情")
	@RequestMapping(value = "admin/examPaper/{id}",method = RequestMethod.GET)
	public Result GetExamPaper(@PathVariable("id")Integer id) {
		ExamPaper examPaper = examPaperService.GetExamPaper(id);
		if (examPaper == null) {
			return Result.failure(ResultCode.EXAM_PAPER_NOT_EXIST);
		} else {

			return Result.success(examPaper);
		}
	}
	
	@ApiOperation("管理员修改试卷状态")
	@RequestMapping(value = "admin/examPaper/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateExamPaperState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		ExamPaper examPaper = examPaperService.getById(id);
		if (examPaper == null) {
			return Result.failure(ResultCode.EXAM_PAPER_NOT_EXIST);
		} else {
			try {
				examPaper = examPaperService.SetExamPaperState(id, state);
				return Result.success(examPaper);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	
	@ApiOperation("管理员搜索试卷")
	@RequestMapping(value = "admin/examPaper/search",method = RequestMethod.GET)
	public Result AdminSearchExamPaper(@RequestParam("key")String key,@RequestParam(required = false,defaultValue = "-1")Integer id) {
		return Result.success(examPaperService.SearchExamPaperWithKey(key, false,id));
	}
	
	@ApiOperation("管理员获取试卷列表")
	@RequestMapping(value = "admin/examPapers/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListExamPaper(@PathVariable("page")Integer page,@PathVariable("count")Integer count,
			@RequestParam(value = "type",required = false,defaultValue = "0") Integer type,
			@RequestParam(value = "course",required = false,defaultValue = "0") Integer course,
			@RequestParam(value = "tag",required = false,defaultValue = "0") Integer tag) {





















		return Result.success(examPaperService.ListExamPaperWithTagAndType(count, page, type, tag, false,course));
	}
	
	@ApiOperation("用户获取卷列表")
	@RequestMapping(value = "user/examPapers/{page}/{count}",method = RequestMethod.GET)
	public Result UserListExamPaper(@PathVariable("page")Integer page,@PathVariable("count")Integer count,
			@RequestParam(value = "type",required = false,defaultValue = "0") Integer type,
			@RequestParam(value = "course",required = false,defaultValue = "0") Integer course,
			@RequestParam(value = "tag",required = false,defaultValue = "0") Integer tag) {
		if(!tagServiceImpl.CheckTagExist(tag))
			return Result.failure(ResultCode.TAG_NOT_EXIST);
		else
			return Result.success(examPaperService.ListExamPaperWithTagAndType(count, page, type, tag, true,course));

	}
}

